/**
*
* @ author:JiangChencheng
* @ email:744578095@qq.com
* @ data: 2021-10-27 18:24
*/
export default class Ball extends Laya.Script {

    constructor() {
        super();
        this.rigidBody = null
        this.isTween = false

        this.damage = 1
        this.damageRow = false
    }

    onAwake() {
        this.rigidBody = this.owner.getComponent(Laya.RigidBody)
    }
    
    onUpdate() {
        // 减弱一下往上的速度
        if (this.rigidBody.linearVelocity.y < -20 ) {
            var velocity = this.rigidBody.linearVelocity
            this.rigidBody.linearVelocity = {x:velocity.x*0.8,y:velocity.y*0.8}
        }
    }

    onTriggerExit(object){
        if (object.owner == null) return
        if (object.owner.name == "recover_left") {
            this.recoveryAnimation(50)
        }else if (object.owner.name == "recover_right") {
            this.recoveryAnimation(1030)
        }else if (object.owner.name == "recover_bottom") {
            var value = Math.random()
            if (value <= 0.5) {
                this.owner.pos(270,250)
            }else {
                this.owner.pos(810,250)
            }
        }
    }

    onTriggerEnter(object) {
        // 防止小球检测不到无法回收
        if (object.owner == null) return
        if (object.owner.name == "baffle_left") {
            this.recoveryAnimation(50)
        }else if (object.owner.name == "baffle_right") {
            this.recoveryAnimation(1030)
        }
    }

    recoveryAnimation(x){

        if (this.isTween) return
        // 去掉RigidBody使其不受重力影响
        this.isTween = true
        this.rigidBody.type = "static"
        this.owner.pos(x,1700)
        // 回收动画
        Laya.Tween.to(this.owner, {x:x,y:-70}, 500, null, 
            new Laya.Handler(this,function(){
                Laya.stage.event("RecoverBall")
                this.isTween = false
                this.damage = 1;
                this.damageRow = false;
                this.owner.visible = false
                Laya.Pool.recover("Ball",this.owner)
                // this.owner.destroy()
            })
        )
    }

}